home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Source Code / Libraries / MacTCP Library 1.1 / Library / LowLevel ƒ / Source ƒ / MacTCP.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-12-04  |  3.5 KB  |  160 lines  |  [TEXT/SPM ]

  1. /*
  2.     MacTCP.c
  3.     
  4.     Code to implement the MacTCP driver level information.
  5.     
  6.     01/31/94 dn - Created.
  7.     03/01/94 dn - Modified to call OpenDriver.
  8.     11/28/94 mc - Converted to CodeWarrior
  9.     12/04/95 dn - Prep'd for Apprentice 4
  10. */
  11.  
  12. #include <MyTCPIncludes.h>
  13.  
  14. #include <SegLoad.h>
  15. #include <OSUtils.h>
  16. #include <Traps.h>
  17.  
  18. // local static global variables
  19. static short _gMacTCP=-1;
  20. static ExitToShellUPP _gETSOriginal;
  21.  
  22. // Local prototypes
  23. void MacTCPInstallETSPatch(void);
  24. pascal void ETSZapMacTCP(void);
  25.  
  26. /*
  27.     OpenMacTCP
  28.     
  29.     Opens the MacTCP driver using OpenDriver.  The PBOpen way depended greatly upon the permissions passed
  30.     to the driver.  If they were not set correctly, the one of the apps could open the driver first, but any other
  31.     app could not get to it because of invalid permissions.
  32. */
  33. OSErr OpenMacTCP(short* drv,Boolean patch){
  34.     OSErr err=noErr;
  35.     
  36.     *drv=0;
  37.     
  38.     if (_gMacTCP!=-1){
  39.         // the driver is already open...
  40.         *drv=_gMacTCP;
  41.         return noErr;
  42.     }
  43.     
  44.     // else the driver hasn't been opened yet...
  45.     err=OpenDriver("\p.IPP",&_gMacTCP);
  46.     
  47.     if (err!=noErr)
  48.         return err;
  49.     
  50.     *drv=_gMacTCP;
  51.     
  52.     if (patch) // only install the patch once....
  53.         MacTCPInstallETSPatch();
  54.     
  55.     return err;
  56. }
  57.  
  58. /*
  59.     KillMacTCP
  60.     
  61.     Kills any open streams that the application has open, but only if the library was used to open
  62.     the MacTCP driver.
  63.     
  64.     This code comes from DTS's ZapTCP application.  It is slightly modified to:
  65.         a.  Work automatically if the library is used to open MacTCP.
  66.         b.  Provide an external function call for external use.
  67.         c.  Removes notification of closed/terminated streams.
  68.         d.  Optimized.
  69.         e.  Reformated and reorganized.
  70. */
  71. OSErr KillMacTCP(void){
  72.     THz theZone;
  73.     OSErr err;
  74.     TCPiopbPtr tcp;
  75.     StreamPtr *curStream,stream;
  76.     long theStream;
  77.     unsigned long index,max;
  78.     
  79.     if (_gMacTCP==-1) // exit if the library didn't open MacTCP
  80.         return noErr;
  81.     
  82.     theZone = ApplicZone();
  83.     
  84.     // call TCPGlobalInfo, which returns a list of the open connections and streams
  85.     tcp=NewTCPiopbPtr();
  86.     
  87.     tcp->ioCRefNum=_gMacTCP;
  88.     err=TCP_GlobalInfo(tcp,false);
  89.         
  90.     if (err!=noErr)
  91.         return err;
  92.     
  93.     // check each stream to see if its buffers are in our application heap.  if so,
  94.     // release the connection via TCPAbort and TCPRelease.
  95.     
  96.     max = (*tcp).csParam.globalInfo.tcpParamPtr->tcpMaxConn;
  97.     
  98.     if (max>0){
  99.         curStream = (*tcp).csParam.globalInfo.tcpCDBTable[0];
  100.         
  101.         for (index=0; index<max; index++,curStream++) {
  102.             
  103.             stream=*curStream;
  104.             theStream = (long)*curStream;
  105.             
  106.             if ((theStream)&&((theStream%2)==0)&&(PtrZone((Ptr)theStream)==theZone)){
  107.                 // only release streams in our heap 
  108.                 
  109.                 // abort connection 
  110.                 tcp->tcpStream = stream;
  111.                 err = TCP_Abort(tcp,false);
  112.                 
  113.                 // release stream
  114.                 tcp->tcpStream = stream;
  115.                 err = TCP_Release(tcp,false);
  116.             }
  117.         }
  118.     }
  119.     
  120.     DisposeTCPiopbPtr(tcp);
  121.     
  122.     return err;
  123. }
  124.  
  125. /*
  126.     MacTCPInstallETSPatch
  127.     
  128.     Installs a patch on ExitToShell to ensure that our routine is called.
  129. */
  130. void MacTCPInstallETSPatch(void){
  131.     ExitToShellUPP ets;
  132.     
  133.     ets=NewExitToShellProc(ETSZapMacTCP);
  134.     
  135.     _gETSOriginal=(ExitToShellUPP)NGetTrapAddress(_ExitToShell,ToolTrap);
  136.     NSetTrapAddress((UniversalProcPtr)ets,_ExitToShell,ToolTrap);
  137. }
  138.  
  139. /*
  140.     ETSZapMacTCP
  141.     
  142.     Routine called in place of the original ExitToShell procedure.  It calls KillMacTCP to dispose
  143.     of any open streams, then it calls the original ExitToShell procedure to allow the process to continue.
  144. */
  145. pascal void ETSZapMacTCP(void){
  146.     long savedA5=SetCurrentA5();
  147.     OSErr err;
  148.     
  149.     err=KillMacTCP();
  150.     
  151.     SetA5(savedA5);
  152.     
  153.     CallExitToShellProc(_gETSOriginal);
  154. }
  155.  
  156.  
  157.  
  158.  
  159.  
  160.